#include <stdio.h>
#include <stdlib.h>
#include "../../include/type_list.h"
#include "../../src/common/func.h"
#include "../../src/linear_list/linear_list.h"
#include "../../src/linear_list/union_merge_linear_list.h"

void testInsert(Sqlist & L,int arr [3]){
    //从第一个位置插入 23    
    ListInsert_Sq(L,1,arr[0]);
    // ListPrint_sq(L);                                 //L.elem = 23           length = 1 , listsize = 100 

    ListInsert_Sq(L,2,arr[1]);
    ListInsert_Sq(L,3,arr[2]);
    // ListPrint_sq(L);                                 //L.elem = 77,45,23     length = 3 , listsize = 100 
}

void testDelete(Sqlist & L){
    ElemType deletedEle;
    ListDelete_Sq(L,2,deletedEle);
    printf("L deleted element is: %d \n", deletedEle);  //L deleted element is: 45 
}

void testUnion(List & L1,List & L2){
    // 将 L2 合并到 L1 中
    Union(L1,L2);

    ListPrint_sq(L1);
    // 1,2,3,4,5
    // length = 5 , listsize = 100

    ListPrint_sq(L2);
    // 2,4,5
    // length = 3 , listsize = 100
}

void testMergeList(List & L1,List & L2){
    List L3 = {};
    //将 L1 和 L2 的数据合并到 L3
    Mergelist(L1,L2,L3);

    //1,2,3,4,5 + 2,4,5 = 1,2,2,3,4,4,5,5 ，Mergelist() 并不会去重
    ListPrint_sq(L3);
    //1,2,2,3,4,4,5,5
    //length = 8 , listsize = 100
}

void  testMergelist_Sq (){

    Sqlist L1 = {};
    Status init_result = Initlist_Sq(L1);
    printf("init_result L1 = %d \n",init_result);       // init_result = 0 

    int arr [3] = {23,45,77};
    testInsert(L1,arr);
    // testDelete(L1);
    ListPrint_sq(L1);                                   // L.elem = 77,23                   length = 2 , listsize = 100 
   
    Sqlist L2 = {};
    Status init_result2 = Initlist_Sq(L2);
    printf("init_result2 L2 = %d \n",init_result2);       

    int arr2 [3] = {77,99,100};
    testInsert(L2,arr2);
    ListPrint_sq(L2);                                   //

    printf("----- Mergelist_sq ------ \n");
    Sqlist L3 = {};
    Mergelist_Sq(L1,L2,L3);                             
    ListPrint_sq(L3);                                   // L.elem = 23,45,77,77,99,100       length = 6 , listsize = 6

    freeMemory(L1);
    freeMemory(L2);
    freeMemory(L3);
}

void  total_test (){

    //初始化 L1
    List L1 = {};
    InitList(L1);
    L1.elem[0] = 1;
    L1.elem[1] = 2;
    L1.elem[2] = 3;
    L1.length = 3;

    //初始化 L2
    List L2 = {};
    InitList(L2);
    ListInsert(L2,1,2);
    ListInsert(L2,2,4);
    ListInsert(L2,3,5);

    testUnion(L1,L2);
    testMergeList(L1,L2);

    freeMemory(L1);
    freeMemory(L2);
}